<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .wp {
            width: 500px;
            background-color: blue;
            color: #fff;
            text-align: center;
            padding: 20px;
            margin: 100px;
        }
        
        .text-info {
            width: 100%;
            margin-bottom: 30px;
        }
        
        .text-info>div:nth-child(1) {
            float: left;
        }
        
        .text-info>div:nth-child(2) {
            float: right;
        }
        
        .text-info>div:nth-child(2)>span {
            color: red;
        }
        
        textarea {
            width: 100%;
            height: 80px;
        }
        
        button.publish {
            height: 30px;
            width: 90px;
            margin-top: 20px;
            outline: none;
            background-color: purple;
            color: #fff;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        
        .content {
            border-bottom: 1px dotted #ccc;
            overflow: hidden;
            height: 50px;
            line-height: 50px;
        }
        
        .content>p {
            float: left;
        }
        
        .content>button {
            float: right;
            cursor: pointer;
            margin-top: 15px;
        }
    </style>
</head>

<body>
    <div class="wp">
        <div class="text-info">
            <div>有什么新鲜事告诉大家</div>
            <div>您还可以输入<span class="red-color">140</span>个字</div>
        </div>

        <div>
            <textarea name="" id="" cols="30" rows="10" placeholder="请输入内容"></textarea>
        </div>
        <button class="publish">发布</button>
        <!-- <div class="content">
            <p>123456</p>
            <button class="del">删除</button>
        </div> -->
    </div>

    <script>
        ! function(win, doc) {
            var text = doc.querySelector('textarea');
            var pubBtn = doc.querySelector('.publish');
            var delBtn = doc.querySelector('.del');
            var lastText = doc.querySelector('.red-color');
            var initNum = lastText.innerHTML;
            var wp = doc.querySelector('.wp');

            text.onkeyup = function() {
                var val = this.value;
                var len = val.length;
                lastText.innerHTML = initNum - len;
            }
            pubBtn.onclick = function() {
                var val = text.value;
                if (val.length == 0) {
                    alert('请输入内容');
                    return;
                }
                renderPublisher(val);
            }

            function renderPublisher(val) {
                var div = doc.createElement('div');
                div.className = 'content';
                div.innerHTML = `<p>${val}</p><button class="del">删除</button>`;
                wp.appendChild(div);
            }

            wp.onclick = function(e) {
                var target = e.target;
                if (target.className != 'del') {
                    return;
                }
                if (confirm('确定要删除么?')) {
                    target.parentNode.parentNode.removeChild(target.parentNode);
                }
            }
        }(window, document);
    </script>
</body>

</html>